/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-17     hehung       the first version
 */

/*
 * Use the packages ssd1306
 * */

#include "app_oled.h"
#include "bmp.h"
#include "board.h"
#include "ssd1306.h"
#include <stdio.h>
#include "codetab.h"

#if 0
#define ESP8266_OLED_DEBUG
#endif
#if 0
#define ESP8266_OLED_ESP8266_INIT_DEBUG
#endif



static void OLED_ManageEntry(void *parameter);
static void OLED_ThreadManage(void);
static void OLED_DrawBmpImg(uint8_t x,
                            uint8_t y,
                            SSD1306_COLOR color,
                            const uint8_t bmp[][16],
                            uint32_t len);
static void OLED_DrawBmpImg2(uint8_t x,
                            uint8_t y,
                            SSD1306_COLOR color,
                            const uint8_t bmp[][8],
                            uint32_t len,
                            uint8_t size);
static void OLED_Hz_String(uint8_t x,
                           uint8_t y,
                           uint8_t chr_S,
                           uint8_t chr_E,
                           SSD1306_COLOR color);
static void OLED_DrawHz(uint8_t x,
                        uint8_t y,
                        uint8_t chr,
                        SSD1306_COLOR color);
static void OLED_ShowTime(void);
static void OLED_ShowSetting(void);
static void OLED_ShowWifiSetting(void);
static void OLED_ShowTimeSetting(void);
static void OLED_ShowDataDownload(void);
static void OLED_ShowMsgIco(void);

/*
 * System startup display
 * */
void OLED_AppInit(void)
{
    OLED_ThreadManage();
}

/**/
static void OLED_ManageEntry(void *parameter)
{
    ssd1306_Init();    /*IIC and OLED initial*/

    OLED_DrawHz(0, 24, 0, White);     /*0 - 你*/
    OLED_DrawHz(16, 24, 1, White);    /*1 - 好*/
    OLED_DrawHz(32, 24, 2, White);   /*2 - ，*/
    OLED_DrawHz(48, 24, 3, White);     /*3 - 世*/
    OLED_DrawHz(64, 24, 4, White);     /*4 - 界*/
    ssd1306_UpdateScreen();
    rt_thread_mdelay(1000);

    ssd1306_Fill(Black);
    OLED_DrawBmpImg(0, 10, White, EleFans_logo, Get_EleFans_logo_len());
    ssd1306_UpdateScreen();
    rt_thread_mdelay(1000);

    OLED_ShowSetting();

    while(1)
    {

        rt_thread_mdelay(100);
    }
}

static void OLED_ShowSetting(void)
{
    ssd1306_Fill(Black);
    OLED_DrawBmpImg2(24, 8, White, Setting_48X48, Get_Setting_48X48_len(), 48);
    ssd1306_UpdateScreen();
}

static void OLED_ShowWifiSetting(void)
{
    ssd1306_Fill(Black);
    OLED_DrawBmpImg2(24, 8, White, Wifi_48X48, Get_Wifi_48X48_len(), 48);
    ssd1306_UpdateScreen();
}

static void OLED_ShowTimeSetting(void)
{
    ssd1306_Fill(Black);
    OLED_DrawBmpImg2(24, 8, White, Clock_48X48, Get_Clock_48X48_len(), 48);
    ssd1306_UpdateScreen();
}

static void OLED_ShowMsgIco(void)
{
    ssd1306_Fill(Black);
    OLED_DrawBmpImg2(24, 8, White, Message_48X48, Get_Message_48X48_len(), 48);
    ssd1306_UpdateScreen();
}

static void OLED_ShowWifiRetry(void)
{
    ssd1306_Fill(Black);
    OLED_DrawBmpImg2(0, 0, White, Wifi_24X24, Get_Wifi_24X24_len(), 24);
    OLED_Hz_String(23, 0, 38, 43, White);
    OLED_Hz_String(48, 28, 47, 48, Black);
    ssd1306_UpdateScreen();
}

static void OLED_ShowDateTimeRetry(void)
{
    ssd1306_Fill(Black);
    OLED_DrawBmpImg2(0, 0, White, Clock_24X24, Get_Clock_24X24_len(), 24);
    OLED_Hz_String(23, 0, 32, 37, White);
    OLED_Hz_String(48, 28, 47, 48, Black);
    ssd1306_UpdateScreen();
}

static void OLED_ThreadManage(void)
{
    rt_thread_t oled_thread;

    oled_thread = rt_thread_create("OLED Thread Manage",     /*线程名字*/
                                   OLED_ManageEntry,         /*线程入口函数*/
                                   RT_NULL,                  /*线程入口函数参数*/
                                   4096,                     /*线程栈大小*/
                                   8,                        /*线程优先级*/
                                   10);                      /*线程时间片*/

    if(oled_thread != RT_NULL)
    {
        rt_kprintf("OLED Thread Created Success!\n");
        rt_thread_startup (oled_thread);
    }
    else
    {
        rt_kprintf("OLED Thread Create Failed!\n");
    }
}

/*Display more than one hanzi*/
static void OLED_Hz_String(uint8_t x,
                           uint8_t y,
                           uint8_t chr_S,
                           uint8_t chr_E,
                           SSD1306_COLOR color)
{
    uint8_t i;

    for(i=chr_S; i<=chr_E; i++)
    {
        OLED_DrawHz(x+16*(i-chr_S), y, i, color);
    }
}

/*Display a HanZi*/
static void OLED_DrawHz(uint8_t x,
                        uint8_t y,
                        uint8_t chr,
                        SSD1306_COLOR color)
{
    uint8_t temp,t,t1;
    uint8_t y0=y;
    uint8_t csize=32u;

    chr <<= 1u;

    for(t=0; t<csize; t++)
    {
        if(t == 16u)
            chr += 1;

        temp=(White == color) ? (Hz[chr][t%16]):(~Hz[chr][t%16]);
//        temp=Hz[chr][t];
        for(t1=0;t1<8;t1++)
        {
            if(temp&0x80)
                ssd1306_DrawPixel(x, y, White);

            temp<<=1;
            y++;
            if((y-y0) == 16)
            {
                y=y0;
                x++;
                break;
            }
        }
    }
}

static void OLED_DrawBmpImg(uint8_t x,
                            uint8_t y,
                            SSD1306_COLOR color,
                            const uint8_t bmp[][16],
                            uint32_t len)
{
    uint16_t t,t1;
    uint8_t  y0 = y;
    uint8_t  temp = 0u;
    uint16_t len2 = (uint16_t)(len>>4u);

    for(t=0; t<len; t++)
    {
        temp=(White == color) ?
             (bmp[(uint8_t)(t/16)][(uint8_t)(t%16)]):(~bmp[(uint8_t)(t/16)][(uint8_t)(t%16)]);

        for(t1=0; t1<8; t1++)
        {
            if(temp&0x80)
                ssd1306_DrawPixel(x, y, White);

            temp <<= 1u;
            y++;
            if((y-y0) == len2)
            {
                y = y0;
                x ++;
                break;
            }
        }
    }
}

static void OLED_DrawBmpImg2(uint8_t x,
                             uint8_t y,
                             SSD1306_COLOR color,
                             const uint8_t bmp[][8],
                             uint32_t len,
                             uint8_t size)
{
    uint16_t t, t1;
    uint8_t  y_end;
    uint8_t  temp;
    uint16_t len2 = 0u;

    y_end = y;

    if(size == 24)
        len2 = (uint16_t)(len/3.2);
    else if(size == 16)
        len2 = (uint16_t)(len/2);
    else if(size == 48)
        len2 = (uint16_t)(len/6);
    else if(size == 64)
        len2 = (uint16_t)(len/16);

    for(t=0; t<len; t++)
    {
        temp=(White == color) ?
             (bmp[(uint8_t)(t/8)][(uint8_t)(t%8)]):(~bmp[(uint8_t)(t/8)][(uint8_t)(t%8)]);

        for(t1=0; t1<8; t1++)
        {
            if(temp&0x80)
                ssd1306_DrawPixel(x, y, White);

            temp <<= 1u;
            y++;
            if((y-y_end) == len2)
            {
                y = y_end;
                x ++;
                break;
            }
        }
    }
}

